package org.lb.MarketingSystem.serviceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.task.Task;
import org.lb.MarketingSystem.service.LeaveProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component(value="leaveProcessService")
public class LeaveProcessServiceImpl implements LeaveProcessService{
	@Autowired
	private RepositoryService repositoryService;
	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private TaskService taskService;
	@Autowired
	private HistoryService historyService;
	/**
	 * 发布流程
	 */
	public void develop(){
		DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
		deploymentBuilder.addClasspathResource("LeaveProcess.bpmn").addClasspathResource("LeaveProcess.png").name("请假流程");
		deploymentBuilder.deploy();
	}
	
	/**
	 * 启动流程
	 * @param map:
	 * 		assignee:处理人，msg:备注
	 */
	public void start(Map<String, Object> map){
		//流程定义key
		String processDefinitionKey="LeaveProcess";
		runtimeService.startProcessInstanceByKey(processDefinitionKey,map);
	}
	
	/**
	 * 查询任务
	 */
	public List<Map<String, Object>> queryTask(String assignee){
		List<Task> list = taskService.createTaskQuery().taskAssignee(assignee).orderByTaskCreateTime().desc().list();
		List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
		for (Task task : list) {
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("task", task);
			Map<String, Object> variables = taskService.getVariables(task.getId());
			map.put("variables", variables);
			tasks.add(map);
		}
		return tasks;
	}
	
	/**
	 * 办理任务
	 * @param 
	 * 		taskId任务Id
	 * 		map:
	 * 		assignee:办理人,msg:备注,flag:是否同意审批
	 */
	public void complete(String taskId,Map<String, Object> map){
		taskService.complete(taskId,map);
	}
	
	/**
	 * 查询处理结果
	 */
	public Map<String, Object> getHistoryvarinsByAssignee(String assignee){
		List<HistoricTaskInstance> taskInstances = historyService.createHistoricTaskInstanceQuery().taskAssignee(assignee).finished().orderByHistoricTaskInstanceEndTime().desc().list();
		HistoricTaskInstance hTaskInstance = taskInstances.get(0);
		Map<String,Object> map = new HashMap<String, Object>();
		List<HistoricVariableInstance> list = historyService.createHistoricVariableInstanceQuery().processInstanceId(hTaskInstance.getProcessInstanceId()).list();
		for (HistoricVariableInstance variableInstance : list) {
			map.put(variableInstance.getVariableName(), variableInstance.getValue());
		}
		return map;
	}

	/**
	 * 获取任务id
	 */
	public String getTaskIdByAssignee(String assignee) {
		Task task = taskService.createTaskQuery().taskAssignee(assignee).singleResult();
		return task.getId();
	}
}
